package com.zhkj.txg.module.mine.ui

import android.view.View
import android.webkit.WebView
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.vm.SettingViewModel
import com.zhkj.txg.module.product.ui.WatcherActivity
import com.zhkj.txg.utils.WebViewUtils
import com.zhkj.txg.utils.WebViewUtils.addImageClickListener
import kotlinx.android.synthetic.main.activity_welfare.*

class AboutUsActivity : ViewModelActivity<SettingViewModel>() {
    override fun setViewModel(): SettingViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleWelfare
    override val layoutResId: Int = R.layout.activity_welfare

    private val webView: WebView by lazy { WebView(this) }

    override fun initUI() {
        titleWelfare.setTitleTextContent(getString(R.string.system_about))
        WebViewUtils.initWebView(webView)
        webView.addImageClickListener {
            if (!it.isNullOrEmpty()) WatcherActivity.open(this, arrayListOf(it), 0)
        }
        WebViewUtils.addWebView2Container(webView, container)
    }

    override fun initData() {
        viewModel.aboutUs()
    }

    override fun initViewModel() {
        viewModel.aboutUsLiveData.observe(this, Observer { response ->
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                WebViewUtils.loadFullScreenHtml(webView, it.content)
            }
        })
    }

    override fun onDestroy() {
        WebViewUtils.removeWebView(webView, container)
        super.onDestroy()
    }
}